package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.util.ApiUtils;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //map转化成类
        Hospital hospital = ApiUtils.mapToClass(paramMap, Hospital.class);
        //查看是否存在
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        //如果存在 进行修改,不存在就进行添加
        if(hospitalExist!=null){
            hospitalUpdate(hospital, hospitalExist);
            return;
        }
        hospitalSave(hospital);
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        //其实应该要写健壮性判断的吧
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = getHospitalByHospitalQueryVo(hospitalQueryVo);
        //PageAble对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        //Example对象
        Example<Hospital> example = getHospitalExample(hospital);
        //返回
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        pages.getContent().forEach(item -> {
            //这里面进行了远程调用
            this.setHospitalHosType(item);
        });
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        if(hospital==null){
            //TODO 抛出异常
        }
        Hospital hospitalAllDetail = setHospitalHosType(hospital);
        Map<String,Object> result = getHospMap(hospitalAllDetail);
        return result;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        //查询医院
        Hospital hospital = getByHoscode(hoscode);
        //封装详情
        Hospital hospitalDetail = setHospitalHosType(hospital);
        Map<String, Object>  result = getHospMap(hospitalDetail);
        return result;
    }

    private Map<String,Object> getHospMap(Hospital hospitalAllDetail) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("hospital", hospitalAllDetail);
        //单独处理更直观
        map.put("bookingRule", hospitalAllDetail.getBookingRule());
        //不需重复返回
        hospitalAllDetail.setBookingRule(null);
        return map;
    }

    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dicCode和value获取医院名称
        String hostype = dictFeignClient.getName("Hostype", hospital.getHostype());
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        String city = dictFeignClient.getName(hospital.getCityCode());
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString",hostype);
        hospital.getParam().put("fullAddress",province+city+district);
        return hospital;
    }

    private Hospital getHospitalByHospitalQueryVo(HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        return hospital;
    }

    private Example<Hospital> getHospitalExample(Hospital hospital) {
        ExampleMatcher matcher = getHospitalMatcher();
        //创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        return example;
    }

    private ExampleMatcher getHospitalMatcher() {
        //Matcher对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        return matcher;
    }

    //新hospital
    private void hospitalSave(Hospital hospital) {
        hospital.setStatus(0);
        hospital.setCreateTime(new Date());
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);
    }
    //修改hospital
    private void hospitalUpdate(Hospital hospital, Hospital hospitalExist) {
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

}
